// 戒烟助手前端交互脚本

// 计时器功能
class QuitSmokingTimer {
    constructor(quitDate) {
        this.quitDate = new Date(quitDate);
        this.startTimer();
    }

    startTimer() {
        setInterval(() => {
            this.updateDisplay();
        }, 1000);
    }

    updateDisplay() {
        const now = new Date();
        const diff = now - this.quitDate;
        
        const days = Math.floor(diff / (1000 * 60 * 60 * 24));
        const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
        const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        const seconds = Math.floor((diff % (1000 * 60)) / 1000);

        // 更新页面显示
        const timerElement = document.getElementById('quit-timer');
        if (timerElement) {
            timerElement.innerHTML = `
                <div class="timer-display">
                    <span class="time-unit">${days}天</span>
                    <span class="time-unit">${hours}小时</span>
                    <span class="time-unit">${minutes}分钟</span>
                    <span class="time-unit">${seconds}秒</span>
                </div>
            `;
        }
    }
}

// 统计数据动画
function animateStats() {
    const statElements = document.querySelectorAll('.stat-number');
    statElements.forEach(element => {
        const finalValue = parseInt(element.textContent);
        let currentValue = 0;
        const increment = finalValue / 50;
        
        const timer = setInterval(() => {
            currentValue += increment;
            if (currentValue >= finalValue) {
                currentValue = finalValue;
                clearInterval(timer);
            }
            element.textContent = Math.floor(currentValue);
        }, 50);
    });
}

// DOM加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 初始化计时器（如果存在戒烟日期）
    const quitDateElement = document.getElementById('quit-date');
    if (quitDateElement) {
        const quitDate = quitDateElement.dataset.quitDate;
        if (quitDate) {
            new QuitSmokingTimer(quitDate);
        }
    }
    
    // 启动统计动画
    animateStats();
});

// 导出全局函数
window.QuitSmokingTimer = QuitSmokingTimer;